<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>youtube.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_media_youtube.js.html">youtube.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview provides a reusable youtube UI component given a youtube data
<a name="line17"></a> * model.
<a name="line18"></a> *
<a name="line19"></a> * goog.ui.media.Youtube is actually a {@link goog.ui.ControlRenderer}, a
<a name="line20"></a> * stateless class - that could/should be used as a Singleton with the static
<a name="line21"></a> * method {@code goog.ui.media.Youtube.getInstance} -, that knows how to render
<a name="line22"></a> * youtube videos. It is designed to be used with a {@link goog.ui.Control},
<a name="line23"></a> * which will actually control the media renderer and provide the
<a name="line24"></a> * {@link goog.ui.Component} base. This design guarantees that all different
<a name="line25"></a> * types of medias will behave alike but will look different.
<a name="line26"></a> *
<a name="line27"></a> * goog.ui.media.Youtube expects {@code goog.ui.media.YoutubeModel} on
<a name="line28"></a> * {@code goog.ui.Control.getModel} as data models, and render a flash object
<a name="line29"></a> * that will play that URL.
<a name="line30"></a> *
<a name="line31"></a> * Example of usage:
<a name="line32"></a> *
<a name="line33"></a> * &lt;pre&gt;
<a name="line34"></a> *   var video = goog.ui.media.YoutubeModel.newInstance(
<a name="line35"></a> *       &#39;http://www.youtube.com/watch?v=ddl5f44spwQ&#39;);
<a name="line36"></a> *   goog.ui.media.Youtube.newControl(video).render();
<a name="line37"></a> * &lt;/pre&gt;
<a name="line38"></a> *
<a name="line39"></a> * youtube medias currently support the following states:
<a name="line40"></a> *
<a name="line41"></a> * &lt;ul&gt;
<a name="line42"></a> *   &lt;li&gt; {@link goog.ui.Component.State.DISABLED}: shows &#39;flash not available&#39;
<a name="line43"></a> *   &lt;li&gt; {@link goog.ui.Component.State.HOVER}: mouse cursor is over the video
<a name="line44"></a> *   &lt;li&gt; {@link !goog.ui.Component.State.SELECTED}: a static thumbnail is shown
<a name="line45"></a> *   &lt;li&gt; {@link goog.ui.Component.State.SELECTED}: video is playing
<a name="line46"></a> * &lt;/ul&gt;
<a name="line47"></a> *
<a name="line48"></a> * Which can be accessed by
<a name="line49"></a> * &lt;pre&gt;
<a name="line50"></a> *   youtube.setEnabled(true);
<a name="line51"></a> *   youtube.setHighlighted(true);
<a name="line52"></a> *   youtube.setSelected(true);
<a name="line53"></a> * &lt;/pre&gt;
<a name="line54"></a> *
<a name="line55"></a> * This package also provides a few static auxiliary methods, such as:
<a name="line56"></a> *
<a name="line57"></a> * &lt;pre&gt;
<a name="line58"></a> * var videoId = goog.ui.media.Youtube.parseUrl(
<a name="line59"></a> *     &#39;http://www.youtube.com/watch?v=ddl5f44spwQ&#39;);
<a name="line60"></a> * &lt;/pre&gt;
<a name="line61"></a> *
<a name="line62"></a> *
<a name="line63"></a> * @supported IE6, FF2+, Safari. Requires flash to actually work.
<a name="line64"></a> *
<a name="line65"></a> * TODO(user): test on other browsers
<a name="line66"></a> */
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>goog.provide(&#39;goog.ui.media.Youtube&#39;);
<a name="line70"></a>goog.provide(&#39;goog.ui.media.YoutubeModel&#39;);
<a name="line71"></a>
<a name="line72"></a>goog.require(&#39;goog.string&#39;);
<a name="line73"></a>goog.require(&#39;goog.ui.Component.Error&#39;);
<a name="line74"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line75"></a>goog.require(&#39;goog.ui.media.FlashObject&#39;);
<a name="line76"></a>goog.require(&#39;goog.ui.media.Media&#39;);
<a name="line77"></a>goog.require(&#39;goog.ui.media.MediaModel&#39;);
<a name="line78"></a>goog.require(&#39;goog.ui.media.MediaModel.Player&#39;);
<a name="line79"></a>goog.require(&#39;goog.ui.media.MediaModel.Thumbnail&#39;);
<a name="line80"></a>goog.require(&#39;goog.ui.media.MediaRenderer&#39;);
<a name="line81"></a>
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Subclasses a goog.ui.media.MediaRenderer to provide a Youtube specific media
<a name="line86"></a> * renderer.
<a name="line87"></a> *
<a name="line88"></a> * This class knows how to parse youtube urls, and render the DOM structure
<a name="line89"></a> * of youtube video players and previews. This class is meant to be used as a
<a name="line90"></a> * singleton static stateless class, that takes {@code goog.ui.media.Media}
<a name="line91"></a> * instances and renders it. It expects {@code goog.ui.media.Media.getModel} to
<a name="line92"></a> * return a well formed, previously constructed, youtube video id, which is the
<a name="line93"></a> * data model this renderer will use to construct the DOM structure.
<a name="line94"></a> * {@see goog.ui.media.Youtube.newControl} for a example of constructing a
<a name="line95"></a> * control with this renderer.
<a name="line96"></a> *
<a name="line97"></a> * goog.ui.media.Youtube currently supports all {@link goog.ui.Component.State}.
<a name="line98"></a> * It will change its DOM structure between SELECTED and !SELECTED, and rely on
<a name="line99"></a> * CSS definitions on the others. On !SELECTED, the renderer will render a
<a name="line100"></a> * youtube static &lt;img&gt;, with a thumbnail of the video. On SELECTED, the
<a name="line101"></a> * renderer will append to the DOM a flash object, that contains the youtube
<a name="line102"></a> * video.
<a name="line103"></a> *
<a name="line104"></a> * This design is patterned after http://go/closure_control_subclassing
<a name="line105"></a> *
<a name="line106"></a> * It uses {@link goog.ui.media.FlashObject} to embed the flash object.
<a name="line107"></a> *
<a name="line108"></a> * @constructor
<a name="line109"></a> * @extends {goog.ui.media.MediaRenderer}
<a name="line110"></a> */
<a name="line111"></a>goog.ui.media.Youtube = function() {
<a name="line112"></a>  goog.ui.media.MediaRenderer.call(this);
<a name="line113"></a>};
<a name="line114"></a>goog.inherits(goog.ui.media.Youtube, goog.ui.media.MediaRenderer);
<a name="line115"></a>goog.addSingletonGetter(goog.ui.media.Youtube);
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * A static convenient method to construct a goog.ui.media.Media control out of
<a name="line120"></a> * a youtube model. It sets it as the data model goog.ui.media.Youtube renderer
<a name="line121"></a> * uses, sets the states supported by the renderer, and returns a Control that
<a name="line122"></a> * binds everything together. This is what you should be using for constructing
<a name="line123"></a> * Youtube videos, except if you need finer control over the configuration.
<a name="line124"></a> *
<a name="line125"></a> * @param {goog.ui.media.YoutubeModel} youtubeModel The youtube data model.
<a name="line126"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
<a name="line127"></a> *     document interaction.
<a name="line128"></a> * @return {goog.ui.media.Media} A Control binded to the youtube renderer.
<a name="line129"></a> * @suppress {visibility} Calling protected control.setStateInternal().
<a name="line130"></a> */
<a name="line131"></a>goog.ui.media.Youtube.newControl = function(youtubeModel, opt_domHelper) {
<a name="line132"></a>  var control = new goog.ui.media.Media(
<a name="line133"></a>      youtubeModel,
<a name="line134"></a>      goog.ui.media.Youtube.getInstance(),
<a name="line135"></a>      opt_domHelper);
<a name="line136"></a>  control.setStateInternal(goog.ui.Component.State.ACTIVE);
<a name="line137"></a>  return control;
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Default CSS class to be applied to the root element of components rendered
<a name="line143"></a> * by this renderer.
<a name="line144"></a> * @type {string}
<a name="line145"></a> */
<a name="line146"></a>goog.ui.media.Youtube.CSS_CLASS = goog.getCssName(&#39;goog-ui-media-youtube&#39;);
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Changes the state of a {@code control}. Currently only changes the DOM
<a name="line151"></a> * structure when the youtube movie is SELECTED (by default fired by a MOUSEUP
<a name="line152"></a> * on the thumbnail), which means we have to embed the youtube flash video and
<a name="line153"></a> * play it.
<a name="line154"></a> *
<a name="line155"></a> * @param {goog.ui.Control} c The media control.
<a name="line156"></a> * @param {goog.ui.Component.State} state The state to be set or cleared.
<a name="line157"></a> * @param {boolean} enable Whether the state is enabled or disabled.
<a name="line158"></a> * @override
<a name="line159"></a> */
<a name="line160"></a>goog.ui.media.Youtube.prototype.setState = function(c, state, enable) {
<a name="line161"></a>  var control = /** @type {goog.ui.media.Media} */ (c);
<a name="line162"></a>  goog.ui.media.Youtube.superClass_.setState.call(this, control, state, enable);
<a name="line163"></a>
<a name="line164"></a>  // control.createDom has to be called before any state is set.
<a name="line165"></a>  // Use control.setStateInternal if you need to set states
<a name="line166"></a>  if (!control.getElement()) {
<a name="line167"></a>    throw Error(goog.ui.Component.Error.STATE_INVALID);
<a name="line168"></a>  }
<a name="line169"></a>
<a name="line170"></a>  var domHelper = control.getDomHelper();
<a name="line171"></a>  var dataModel =
<a name="line172"></a>      /** @type {goog.ui.media.YoutubeModel} */ (control.getDataModel());
<a name="line173"></a>
<a name="line174"></a>  if (!!(state &amp; goog.ui.Component.State.SELECTED) &amp;&amp; enable) {
<a name="line175"></a>    var flashEls = domHelper.getElementsByTagNameAndClass(
<a name="line176"></a>        &#39;div&#39;,
<a name="line177"></a>        goog.ui.media.FlashObject.CSS_CLASS,
<a name="line178"></a>        control.getElement());
<a name="line179"></a>    if (flashEls.length &gt; 0) {
<a name="line180"></a>      return;
<a name="line181"></a>    }
<a name="line182"></a>    var youtubeFlash = new goog.ui.media.FlashObject(
<a name="line183"></a>        dataModel.getPlayer().getUrl() || &#39;&#39;,
<a name="line184"></a>        domHelper);
<a name="line185"></a>    control.addChild(youtubeFlash, true);
<a name="line186"></a>  }
<a name="line187"></a>};
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>/**
<a name="line191"></a> * Returns the CSS class to be applied to the root element of components
<a name="line192"></a> * rendered using this renderer.
<a name="line193"></a> *
<a name="line194"></a> * @return {string} Renderer-specific CSS class.
<a name="line195"></a> * @override
<a name="line196"></a> */
<a name="line197"></a>goog.ui.media.Youtube.prototype.getCssClass = function() {
<a name="line198"></a>  return goog.ui.media.Youtube.CSS_CLASS;
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * The {@code goog.ui.media.Youtube} media data model. It stores a required
<a name="line205"></a> * {@code videoId} field, sets the youtube URL, and allows a few optional
<a name="line206"></a> * parameters.
<a name="line207"></a> *
<a name="line208"></a> * @param {string} videoId The youtube video id.
<a name="line209"></a> * @param {string=} opt_caption An optional caption of the youtube video.
<a name="line210"></a> * @param {string=} opt_description An optional description of the youtube
<a name="line211"></a> *     video.
<a name="line212"></a> * @constructor
<a name="line213"></a> * @extends {goog.ui.media.MediaModel}
<a name="line214"></a> */
<a name="line215"></a>goog.ui.media.YoutubeModel = function(videoId, opt_caption, opt_description) {
<a name="line216"></a>  goog.ui.media.MediaModel.call(
<a name="line217"></a>      this,
<a name="line218"></a>      goog.ui.media.YoutubeModel.buildUrl(videoId),
<a name="line219"></a>      opt_caption,
<a name="line220"></a>      opt_description,
<a name="line221"></a>      goog.ui.media.MediaModel.MimeType.FLASH);
<a name="line222"></a>
<a name="line223"></a>  /**
<a name="line224"></a>   * The Youtube video id.
<a name="line225"></a>   * @type {string}
<a name="line226"></a>   * @private
<a name="line227"></a>   */
<a name="line228"></a>  this.videoId_ = videoId;
<a name="line229"></a>
<a name="line230"></a>  this.setThumbnails([new goog.ui.media.MediaModel.Thumbnail(
<a name="line231"></a>      goog.ui.media.YoutubeModel.getThumbnailUrl(videoId))]);
<a name="line232"></a>
<a name="line233"></a>  this.setPlayer(new goog.ui.media.MediaModel.Player(
<a name="line234"></a>      this.getFlashUrl(videoId, true)));
<a name="line235"></a>};
<a name="line236"></a>goog.inherits(goog.ui.media.YoutubeModel, goog.ui.media.MediaModel);
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/**
<a name="line240"></a> * A youtube regular expression matcher. It matches the VIDEOID of URLs like
<a name="line241"></a> * http://www.youtube.com/watch?v=VIDEOID. Based on:
<a name="line242"></a> * googledata/contentonebox/opencob/specs/common/YTPublicExtractorCard.xml
<a name="line243"></a> * @type {RegExp}
<a name="line244"></a> * @private
<a name="line245"></a> * @const
<a name="line246"></a> */
<a name="line247"></a>// Be careful about the placement of the dashes in the character classes. Eg,
<a name="line248"></a>// use &quot;[\\w=-]&quot; instead of &quot;[\\w-=]&quot; if you mean to include the dash as a
<a name="line249"></a>// character and not create a character range like &quot;[a-f]&quot;.
<a name="line250"></a>goog.ui.media.YoutubeModel.MATCHER_ = new RegExp(
<a name="line251"></a>    // Lead in.
<a name="line252"></a>    &#39;http://(?:[a-zA-Z]{2,3}\\.)?&#39; +
<a name="line253"></a>    // Watch URL prefix.  This should handle new URLs of the form:
<a name="line254"></a>    // http://www.youtube.com/watch#!v=jqxENMKaeCU&amp;feature=related
<a name="line255"></a>    // where the parameters appear after &quot;#!&quot; instead of &quot;?&quot;.
<a name="line256"></a>    &#39;(?:youtube\\.com/watch)&#39; +
<a name="line257"></a>    // Get the video id:
<a name="line258"></a>    // The video ID is a parameter v=[videoid] either right after the &quot;?&quot;
<a name="line259"></a>    // or after some other parameters.
<a name="line260"></a>    &#39;(?:\\?(?:[\\w=-]+&amp;(?:amp;)?)*v=([\\w-]+)&#39; +
<a name="line261"></a>    &#39;(?:&amp;(?:amp;)?[\\w=-]+)*)?&#39; +
<a name="line262"></a>    // Get any extra arguments in the URL&#39;s hash part.
<a name="line263"></a>    &#39;(?:#[!]?(?:&#39; +
<a name="line264"></a>    // Video ID from the v=[videoid] parameter, optionally surrounded by other
<a name="line265"></a>    // &amp; separated parameters.
<a name="line266"></a>    &#39;(?:(?:[\\w=-]+&amp;(?:amp;)?)*(?:v=([\\w-]+))&#39; +
<a name="line267"></a>    &#39;(?:&amp;(?:amp;)?[\\w=-]+)*)&#39; +
<a name="line268"></a>    &#39;|&#39; +
<a name="line269"></a>    // Continue supporting &quot;?&quot; for the video ID
<a name="line270"></a>    // and &quot;#&quot; for other hash parameters.
<a name="line271"></a>    &#39;(?:[\\w=&amp;-]+)&#39; +
<a name="line272"></a>    &#39;))?&#39; +
<a name="line273"></a>    // Should terminate with a non-word, non-dash (-) character.
<a name="line274"></a>    &#39;[^\\w-]?&#39;, &#39;i&#39;);
<a name="line275"></a>
<a name="line276"></a>
<a name="line277"></a>/**
<a name="line278"></a> * A auxiliary static method that parses a youtube URL, extracting the ID of the
<a name="line279"></a> * video, and builds a YoutubeModel.
<a name="line280"></a> *
<a name="line281"></a> * @param {string} youtubeUrl A youtube URL.
<a name="line282"></a> * @param {string=} opt_caption An optional caption of the youtube video.
<a name="line283"></a> * @param {string=} opt_description An optional description of the youtube
<a name="line284"></a> *     video.
<a name="line285"></a> * @return {goog.ui.media.YoutubeModel} The data model that represents the
<a name="line286"></a> *     youtube URL.
<a name="line287"></a> * @see goog.ui.media.YoutubeModel.getVideoId()
<a name="line288"></a> * @throws Error in case the parsing fails.
<a name="line289"></a> */
<a name="line290"></a>goog.ui.media.YoutubeModel.newInstance = function(youtubeUrl,
<a name="line291"></a>                                                  opt_caption,
<a name="line292"></a>                                                  opt_description) {
<a name="line293"></a>  var extract = goog.ui.media.YoutubeModel.MATCHER_.exec(youtubeUrl);
<a name="line294"></a>  if (extract) {
<a name="line295"></a>    var videoId = extract[1] || extract[2];
<a name="line296"></a>    return new goog.ui.media.YoutubeModel(
<a name="line297"></a>        videoId, opt_caption, opt_description);
<a name="line298"></a>  }
<a name="line299"></a>
<a name="line300"></a>  throw Error(&#39;failed to parse video id from youtube url: &#39; + youtubeUrl);
<a name="line301"></a>};
<a name="line302"></a>
<a name="line303"></a>
<a name="line304"></a>/**
<a name="line305"></a> * The opposite of {@code goog.ui.media.Youtube.newInstance}: it takes a videoId
<a name="line306"></a> * and returns a youtube URL.
<a name="line307"></a> *
<a name="line308"></a> * @param {string} videoId The youtube video ID.
<a name="line309"></a> * @return {string} The youtube URL.
<a name="line310"></a> */
<a name="line311"></a>goog.ui.media.YoutubeModel.buildUrl = function(videoId) {
<a name="line312"></a>  return &#39;http://www.youtube.com/watch?v=&#39; + goog.string.urlEncode(videoId);
<a name="line313"></a>};
<a name="line314"></a>
<a name="line315"></a>
<a name="line316"></a>/**
<a name="line317"></a> * A static auxiliary method that builds a static image URL with a preview of
<a name="line318"></a> * the youtube video.
<a name="line319"></a> *
<a name="line320"></a> * NOTE(user): patterned after Gmail&#39;s gadgets/youtube,
<a name="line321"></a> *
<a name="line322"></a> * TODO(user): how do I specify the width/height of the resulting image on the
<a name="line323"></a> * url ? is there an official API for http://ytimg.com ?
<a name="line324"></a> *
<a name="line325"></a> * @param {string} youtubeId The youtube video ID.
<a name="line326"></a> * @return {string} An URL that contains an image with a preview of the youtube
<a name="line327"></a> *     movie.
<a name="line328"></a> */
<a name="line329"></a>goog.ui.media.YoutubeModel.getThumbnailUrl = function(youtubeId) {
<a name="line330"></a>  return &#39;http://i.ytimg.com/vi/&#39; + youtubeId + &#39;/default.jpg&#39;;
<a name="line331"></a>};
<a name="line332"></a>
<a name="line333"></a>
<a name="line334"></a>/**
<a name="line335"></a> * An auxiliary method that builds URL of the flash movie to be embedded,
<a name="line336"></a> * out of the youtube video id.
<a name="line337"></a> *
<a name="line338"></a> * @param {string} videoId The youtube video ID.
<a name="line339"></a> * @param {boolean=} opt_autoplay Whether the flash movie should start playing
<a name="line340"></a> *     as soon as it is shown, or if it should show a &#39;play&#39; button.
<a name="line341"></a> * @return {string} The flash URL to be embedded on the page.
<a name="line342"></a> */
<a name="line343"></a>goog.ui.media.YoutubeModel.prototype.getFlashUrl = function(videoId,
<a name="line344"></a>                                                            opt_autoplay) {
<a name="line345"></a>  var autoplay = opt_autoplay ? &#39;&amp;autoplay=1&#39; : &#39;&#39;;
<a name="line346"></a>  // YouTube video ids are extracted from youtube URLs, which are user
<a name="line347"></a>  // generated input. the video id is later used to embed a flash object,
<a name="line348"></a>  // which is generated through HTML construction. We goog.string.urlEncode
<a name="line349"></a>  // the video id to make sure the URL is safe to be embedded.
<a name="line350"></a>  return &#39;http://www.youtube.com/v/&#39; + goog.string.urlEncode(videoId) +
<a name="line351"></a>      &#39;&amp;hl=en&amp;fs=1&#39; + autoplay;
<a name="line352"></a>};
<a name="line353"></a>
<a name="line354"></a>
<a name="line355"></a>/**
<a name="line356"></a> * Gets the Youtube video id.
<a name="line357"></a> * @return {string} The Youtube video id.
<a name="line358"></a> */
<a name="line359"></a>goog.ui.media.YoutubeModel.prototype.getVideoId = function() {
<a name="line360"></a>  return this.videoId_;
<a name="line361"></a>};
</pre>


</body>
</html>
